package com.kwq.file.controller;

import com.kwq.common.Common;
import com.kwq.feign.client.MinioClient;
import com.kwq.feign.pojo.User;
import com.kwq.file.pojo.File;
import com.kwq.file.pojo.FileModel;
import com.kwq.file.service.FileService;
import com.kwq.file.service.Impl.MongoDBFileServiceImpl;
import com.kwq.file.service.MongoDBFileService;
import com.kwq.file.utils.MD5Util;
import io.swagger.annotations.ApiOperation;
import org.bson.types.Binary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.function.ToDoubleBiFunction;

public class FileController {

    @Autowired
    MinioClient minioClient;
    @Autowired
    MongoDBFileService mongoDBFileService;
    @Autowired
    FileService fileService;



    // TODO: 2022/8/23 增加api
    @ApiOperation(value = "上传文件")
    @PostMapping("/upLoadMinio")
    public Common upLoad(@RequestParam("file") MultipartFile file) throws IOException, NoSuchAlgorithmException {
        long size = file.getSize();
        String originalname = file.getOriginalFilename();
        String name = file.getName();
        //判断文件类型
        String type = nameCheck(name);
        //图片视频存minio
        if (type.equals("photo")|type.equals("vedio")){
            // DONE: 2022/8/23 minio上传文件
            String path = uploadMinio(file);
            // DONE: 2022/8/23 文件路径存入数据库
            File record = new File();
            record.setCreateTime(new Date());
            record.setName(name);
            record.setPath(path);
            record.setWay("MinIo");
            record.setSize(size);
            fileService.save(record);
        }
        //文档存Mongodb
        if (type.equals("office")){
            // DONE: 2022/8/24 Mongo上传文件
            FileModel fileModel = uploadMongoDb(file);
            // DONE: 2022/8/24 文件存入数据库
            File record = new File();
            record.setCreateTime(new Date());
            record.setCreateTime(new Date());
            record.setName(fileModel.getName());
            record.setWay("MongoDB");
            record.setSize(fileModel.getSize());
            fileService.save(record);

        }
// TODO: 2022/8/24 上传本地





        return new Common(200,"uploadDone");
    }

    // TODO: 下载
    @ApiOperation(value = "查看")
    @PostMapping("/downLoad")
    public Common downLoad(@RequestParam("file") MultipartFile file){




        return null;
    }
    // TODO: 2022/8/24 查看



    //上传minio
    public  String uploadMinio(MultipartFile file){
        Common uploadcommon = minioClient.upload(file);
        String path = (String) uploadcommon.getData();
        return path;
    }

    //上传mongodb
    public FileModel uploadMongoDb(MultipartFile file) throws IOException, NoSuchAlgorithmException {
        FileModel f = new FileModel(file.getOriginalFilename(), file.getContentType(), file.getSize(),
                        new Binary(file.getBytes()));
        f.setMd5(MD5Util.getMD5(file.getInputStream()));
        FileModel fileModel = mongoDBFileService.saveFile(f);
        return fileModel;

    }
    //鉴别文件类型
    public String nameCheck(String name){
        //当文件为图片时
        if (name.endsWith(".jpg")
                || name.endsWith(".jpeg")
                || name.endsWith(".bmp")
                || name.endsWith(".gif")
                || name.endsWith(".png")){
            return "photo";
        } else if(name.endsWith(".mp4")
                || name.endsWith(".flv")
                || name.endsWith(".avi")
                || name.endsWith(".mov")
                || name.endsWith(".wmv")
                || name.endsWith(".m38u")){
            return "vedio";
        }else if(name.endsWith(".doc")
                || name.endsWith(".docx")
                || name.endsWith(".pdf")
                || name.endsWith(".ppt")
                || name.endsWith(".pptx")
                || name.endsWith(".excel")){
            return "office";
        }
        else {
            return "other";
        }
    }




}
